
import { defineStore } from 'pinia'


function getSessionKey(key){
  var a = sessionStorage.getItem("sysmapconfig");
  if(a){
    var b = JSON.parse(a);
    return b[key]
  }
  return undefined
}
export const useConfigStore = defineStore('configStore',{
  state:()=>{
    return {
      GDTK:getSessionKey("GDTK") ,
    TDTTK:getSessionKey("TDTTK") ,
    SUPERMAPURL:getSessionKey("SUPERMAPURL")  
    }
  }
})

export const useMapStoreStore = defineStore('mapStore', {
  state: () => {
    return {
      eqid:'',
      earthquakeInfo:{},
      tree:[],
      radius: [], //半径选择
      layerData:[],
      layerData3d:[],
      subjectData:[],
      isInitCesium:false,
       componentsData :[
  
      ],
      markFlag:false,
      markISArr:[],
      bottomFlag:true,//底部显示人口还是震区
      entityArr:[],
     }
  },
  
  actions: {
    initStore(){
      this.$patch( {
        eqid:'',
      earthquakeInfo:{},
      tree:[],
      radius: [], //半径选择
      layerData:[],
      layerData3d:[],
      subjectData:[],
      isInitCesium:false,
       componentsData :[
  
      ],
      markFlag:false,
      markISArr:[],
      bottomFlag:true,//底部显示人口还是震区
      entityArr:[],
       })
    },
    addEntity(id){
      this.entityArr.push(id);
    },
    delEntity(id){
      var index = this.entityArr.indexOf(id)
      if(index === -1) return
       this.entityArr.splice(index,1);
    },

    addLayer(layer){
     var arr = [...new Set([...layer,...this.layerData])];
     this.$patch({layerData:arr});
    },
    delLayer(layer){
      const newArr = this.layerData.filter(item =>!layer.includes(item));
      this.$patch({ layerData: newArr });
    },
    addTree(layer){
      this.$patch({tree:[...this.tree,layer]});
    },
    delTree(layer){
      const newArr = this.tree.filter(item =>!layer.includes(item));
      this.$patch({ tree: newArr });
    },
    addSubjectData(layer){
      var arr = layer.split(",");
      if(arr && arr.length > 0){
        this.$patch({subjectData:arr})
      }
   },
   
    delSubjectData(layer){
      this.$patch({subjectData:[]})
    },
    increment(newval) {
      this.$patch({radius:newval})
    },
    hideComponents(index){
      const newdata = this.componentsData.filter((e)=> {
        return e.id != index
      })
      
      this.$patch({componentsData:newdata})
    },
    updateComponentsData(newval){
         this.$patch({componentsData:newval})
    },
    addLayer3d(layer){
      this.layerData3d.push(layer);
    },
    delLayer3d(layer){
      var index = 0;
      for (const e of this.layerData3d) {
         if(e == layer){
          this.layerData3d.splice(index,1);
          return
         }
         index++;
      }
    },
  },
})